package com.xkcyy.study.spring.d02_registry;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.*;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;

/**
 * d02_registry
 *
 * @author yuand
 * @date 2021/6/8
 **/
public class D1ImportBean {
    @Import(UserService.class)
    static class Config {
    }

    static class UserService {
        public void test() {
            System.out.println("UserService.test");
        }
    }

    @Test
    void importBean() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(Config.class);
        context.refresh();
        context.getBean(UserService.class).test();
    }

    @Import(UserImportSelector.class)
    static class Config2 {
    }

    static class UserService2 {
        public void test() {
            System.out.println("UserService.test");
        }
    }

    static class UserImportSelector implements ImportSelector {
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{UserService2.class.getName()};
        }
    }

    @Test
    void importBeanBySelector() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(Config2.class);
        context.refresh();
        context.getBean(UserService2.class).test();
    }

    //region 3.ImportBeanDefinitionRegistrar

    @Import(UserImportBeanDefinitionRegistrar.class)
    static class Config3 {
    }

    static class UserService3 {
        public void test() {
            System.out.println("UserService.test");
        }
    }

    static class UserImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            registry.registerBeanDefinition(UserService3.class.getName(),new RootBeanDefinition(UserService3.class));
        }
    }

    @Test
    void importBeanByRegistrar() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(Config3.class);
        context.refresh();
        context.getBean(UserService3.class).test();
    }
    //ednRegion


    // #region zhuce
    @Import(UserService4.class)
    static class Config4 {
    }

    @Conditional(MyCondition.class)
    static class UserService4 {
        public void test() {
            System.out.println("UserService.test");
        }
    }
    static class MyCondition implements Condition{

        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            return true;
        }
    }

    @Test
    void importBean4() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.registerBean(Config4.class);
        context.refresh();
        context.getBean(UserService4.class).test();
    }

    // ednRegion

}
